package com.android.test.view;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;

import com.androidplot.series.XYSeries;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYStepMode;

public class ChartGraph extends XYPlot {
    private static String TAG = "ChartGraph";
    private ArrayList<Number> Xs = new ArrayList<Number>();
    private ArrayList<Number> Ys = new ArrayList<Number>();
    private String xLabel = "";
    private String yLabel = "";
    private String chartNote = "";
    private XYSeries series = null;

    public ChartGraph(Context context, AttributeSet attr) {
	super(context, attr);
	// TODO Auto-generated constructor stub
    }

    public ChartGraph(Context context, String string) {
	super(context, string);
    }

    @SuppressWarnings("unchecked")
    public void setY(ArrayList<Number> inputs) {
	Ys.clear();
	Ys = (ArrayList<Number>) inputs.clone();
	Log.e("zzy", "size = " + Ys.size());
    }

    @SuppressWarnings("unchecked")
    public void setX(ArrayList<Number> inputs) {
	Xs.clear();
	Xs = (ArrayList<Number>) inputs.clone();
	Log.e("zzy", "size = " + Xs.size());
    }

    public void setChartNote(String note) {
	chartNote = note;
    }

    public void setXLabel(String label) {
	xLabel = label;
    }

    public void setYLabel(String label) {
	yLabel = label;
    }

    public void showChart() {
	if (Xs.size() != Ys.size() || Xs.size() == 0 || Ys.size() == 0) {
	    return;
	}
	Log.e("zzy", "showChart");
	if (series != null) {
	    removeSeries(series);
	}

	series = new SimpleXYSeries(Ys.subList(0, Ys.size()), Xs.subList(0,
		Xs.size()), "语文");

	// setup our line fill paint to be a slightly transparent gradient:
	Paint lineFill = new Paint();
	lineFill.setAlpha(200);
	lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE,
		Color.GREEN, Shader.TileMode.MIRROR));

	LineAndPointFormatter formatter = new LineAndPointFormatter(Color.rgb(
		0, 0, 0), Color.BLUE, Color.RED);
	formatter.setFillPaint(lineFill);
	getGraphWidget().setPaddingRight(2);

	addSeries(series, formatter);

	getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
	getGraphWidget().getGridLinePaint().setColor(Color.BLACK);
	getGraphWidget().getGridLinePaint().setPathEffect(
		new DashPathEffect(new float[] { 1, 1 }, 1));
	getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
	getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);

	getBorderPaint().setStrokeWidth(2);
	getBorderPaint().setAntiAlias(true);
	getBorderPaint().setColor(Color.GRAY);

	setRangeStepValue(10);
	setRangeStepMode(XYStepMode.INCREMENT_BY_VAL);
	setRangeBoundaries(0, 100, BoundaryMode.FIXED);
	setRangeLabel(yLabel);
	setRangeValueFormat(new DecimalFormat("0"));

	setDomainStep(XYStepMode.SUBDIVIDE, Xs.size());
	setDomainValueFormat(new DecimalFormat("0"));
	setDomainLabel(xLabel);

	disableAllMarkup();
    }

    private class DateFormat extends Format {

	private static final long serialVersionUID = 1L;

	private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/yyyy");

	@Override
	public StringBuffer format(Object obj, StringBuffer toAppendTo,
		FieldPosition pos) {
	    long timestamp = ((Number) obj).longValue();
	    Date date = new Date(timestamp);
	    return dateFormat.format(date, toAppendTo, pos);
	}

	@Override
	public Object parseObject(String source, ParsePosition pos) {
	    return null;

	}
    }
}